﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Admx_Generator
{
    public class Utilities
    {
        /// <summary>
        /// Get a list of characters composed by [a-z][A-Z][0-9]
        /// </summary>
        /// <returns></returns>
        internal static List<char> GetAllowedCharacters()
        {
            List<char> allowedChar = new List<char>();

            for (int i = 65; i < 91; i++)
            {
                allowedChar.Add((char)i);           // lower letters
                allowedChar.Add((char)(i + 32));    // upper letters
            }
            for (int i = 48; i < 58; i++)
            {
                allowedChar.Add((char)i);           // numbers
            }

            return allowedChar;
        }

        /// <summary>
        /// Get a Guid String where '-' have been replace by '_'.
        /// </summary>
        /// <returns></returns>
        internal static string GetFormattedGuid()
        {
            string formattedGuid = Guid.NewGuid().ToString();

            return formattedGuid.Replace('-', '_');
        }

        /// <summary>
        /// Returns a path where special folder (like %LocalAppData%) have been expanded to the full path.
        /// </summary>
        /// <param name="path">A path to a folder that contains eventually a special folder.</param>
        /// <returns>A path where special folder have been expanded.</returns>
        internal static string GetExpandedPath(string path)
        {
            string expandedPath = String.Empty;

            try
            {
                expandedPath = System.Environment.ExpandEnvironmentVariables(path);
            }
            catch (Exception) { }

            return expandedPath;
        }

        /// <summary>
        /// Test if the given string is a valide filename.
        /// </summary>
        /// <param name="filename">String to test.</param>
        /// <returns>Return true if the given string is a valide filename.</returns>
        public static bool IsValidFilename(string filename)
        {
            char[] invalideCharacters = System.IO.Path.GetInvalidFileNameChars();

            foreach (char character in filename)
            {
                if (invalideCharacters.Contains(character))
                    return false;
            }
            return true;
        }
    }
}
